﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class MenDian_DAL
    {
        public MenDian_DAL(Context context)
        {
            Context = context;
        }

        public Context Context { get; }


        /// <summary>
        /// 门店添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int MenDianAdd(MenDian_Model m)
        {
            var m1=Context.MenDian_Model.FirstOrDefault(x=>x.MName==m.MName);
            if(m1 != null)
            {
                return -1;
            }
            else
            {
                Context.MenDian_Model.Add(m);
                return Context.SaveChanges();
            }
        }


        /// <summary>
        /// 门店列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="MName"></param>
        /// <param name="MPhone"></param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        public List<MenDian_Model> MenDianShow(int index,int size,string MName,string MPhone,out int Count,out int Count2)
        {
            var m=Context.MenDian_Model.Where(x=>x.MIsDeleted=="0").ToList();
            if(!string.IsNullOrEmpty(MName))
            {
                m=m.Where(x=>x.MName.Contains(MName)).ToList();
            }
            if (!string.IsNullOrEmpty(MPhone))
            {
                m = m.Where(x => x.MPhone==MPhone).ToList();
            }
            Count=m.Count();
            Count2=(int)Math.Ceiling(Count*1.0/size);
            return m.OrderBy(x=>x.MId).Skip((index-1)*size).Take(size).ToList();
        }
        /// <summary>
        /// 门店下拉
        /// </summary>
        /// <returns></returns>
        public List<MenDian_Model> MenDianShow2()
        {
            return Context.MenDian_Model.Where(x => x.MIsDeleted == "0").ToList();
        }
        /// <summary>
        /// 门店删除
        /// </summary>
        /// <param name="Mid"></param>
        /// <returns></returns>
        public int MenDianShan(int Mid)
        {
            var m=Context.MenDian_Model.FirstOrDefault(x=>x.MId==Mid);
            m.MIsDeleted = "1";
            return Context.SaveChanges();
        }
    }
}
